package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.CommBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.pull.PullLoadMoreRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LookComActivity extends BaseActivity implements View.OnClickListener {
    private PullLoadMoreRecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_com);
        recycler = (PullLoadMoreRecyclerView) findViewById(R.id.recycler);
        recycler.setLinearLayout();
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("全部评论");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        adapter = new BaseRVAdapter(this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_comment2;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageLoader.getInstance().displayImage(info.get(position).getHead_img(), holder.getImageView(R.id.head));
                holder.setTextView(R.id.name, info.get(position).getRealname());
                holder.setTextView(R.id.time, info.get(position).getAdd_time());
                holder.setTextView(R.id.info, info.get(position).getContent());


                final List<String> img = info.get(position).getImg();
                LinearLayout status1 = holder.getView(R.id.status1);
                LinearLayout status2 = holder.getView(R.id.status2);
                LinearLayout status3 = holder.getView(R.id.status3);
                ImageView img1 = holder.getImageView(R.id.img1);
                ImageView img2_1 = holder.getImageView(R.id.img2_1);
                ImageView img2_2 = holder.getImageView(R.id.img2_2);
                RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                switch (img.size()) {
                    case 0:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);


                        break;
                    case 1:
                        status1.setVisibility(View.VISIBLE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);
                        ImageLoader.getInstance().displayImage(img.get(0),
                                img1);
                        img1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(LookComActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        break;
                    case 2:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.VISIBLE);
                        status3.setVisibility(View.GONE);
                        ImageLoader.getInstance().displayImage(img.get(0),
                                img2_1);
                        ImageLoader.getInstance().displayImage(img.get(1),
                                img2_2);
                        img2_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(LookComActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        img2_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(LookComActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 1);
                                startActivity(intent);
                            }
                        });
                        break;
                    default:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.VISIBLE);
                        img_recycler.setLayoutManager(new GridLayoutManager(LookComActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                        img_recycler.setAdapter(new BaseRVAdapter(LookComActivity.this, img) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.item_photo;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {
                                holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(LookComActivity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", position);
                                        startActivity(intent);
                                    }
                                });
                                ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));
                            }

                        });

                        break;
                }

            }

        };
        recycler.setAdapter(adapter);
        upload(0);
        recycler.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {

            @Override
            public void onRefresh() {
                upload(0);
            }

            @Override
            public void onLoadMore() {
                upload(1);
            }
        });
    }

    private BaseRVAdapter adapter;

    @Override
    protected void onResume() {
        upload(0);
        super.onResume();
    }

    private int pager = 0;
    private List<CommBean.InfoEntity> info = new ArrayList<>();

    private void upload(final int i) {
        if (i == 0) {
            pager = 0;
        } else {
            pager++;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("type", getIntent().getStringExtra("type"));
        map.put("pid", getIntent().getStringExtra("pid"));
        map.put("page", pager);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        XUtil.Post(Url.DECORATE_ALLCOMMENTLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        CommBean bean = gson.fromJson(result, CommBean.class);
                        List<CommBean.InfoEntity> info1 = bean.getInfo();

                        if (i == 0) {
                            info.clear();
                        }
                        adapter.addDataLs(info1);
                        adapter.notifyDataSetChanged();
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                recycler.setPullLoadMoreCompleted();
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
